package com.itheima.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.RoleMapper;
import com.itheima.pojo.Menu;
import com.itheima.pojo.Permission;
import com.itheima.pojo.Role;
import com.itheima.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //告诉mybatis要分页
        Page page = PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        List<Role> roles = roleMapper.findPage(queryPageBean.getQueryString());
        return new PageResult(page.getTotal(), roles);
    }

    @Override
    public List<Menu> findMenuByRoleId(Integer id) {
        //查询角色所关联的菜单
        List<Menu> menus = roleMapper.findMenuByRoleId(id);
        if (CollectionUtil.isNotEmpty(menus)) {
            for (Menu menu : menus) {
                Integer parentMenuId = menu.getId();
                List<Menu> menuList = roleMapper.findChildMenuByParentMenuId(parentMenuId);
                menu.setChildren(menuList);
            }
        }
        return menus;
    }

    @Override
    public Set<Permission> findPermissionByRoleId(Integer id) {
        //查询角色所拥有的权限
        return roleMapper.findPermissionByRoleId(id);
    }

    @Override
    public Result deleteById(Integer id) {
        try {
            //清除角色和权限的关系
            roleMapper.deleteRolePermission(id);
            //删除角色信息
            roleMapper.deleteRole(id);
//            //清除角色和菜单的关系
//            roleMapper.deleteRoleMenu(id);
//            //清除角色和用户关系
//            roleMapper.deleteRoleUser(id);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("删除失败");
        }
        return Result.success("删除成功");
    }

    @Override
    public Role findAll() {
        Role role = new Role();
        //查询所有权限集合
        Set<Permission> permissions = roleMapper.findAllPermission();
        //查询所有菜单信息
        LinkedHashSet<Menu> menus = roleMapper.findAllMenus();
        role.setPermissions(permissions);
        role.setMenus(menus);
        return role;
    }

    @Override
    public void add(Role role) {
        //插入角色基本信息 id回显
        roleMapper.add(role);
        Integer roleId = role.getId();
        List<Integer> permissionIds = role.getPermissionIds();
//        List<Integer> menuIds = role.getMenuIds();
        //建立角色与权限的关系
        setRoleAndPermission(permissionIds, roleId);
        //建立角色与菜单的关系
//        setRoleAndMenu(menuIds,roleId);
    }

    public void setRoleAndPermission(List<Integer> permissionIds, Integer roleId) {
        if (CollectionUtil.isNotEmpty(permissionIds)) {
            List<Map> params = new ArrayList<>();
            for (Integer permissionId : permissionIds) {
                Map map = new HashMap<>();
                map.put("roleId", roleId);
                map.put("permissionId", permissionId);
                params.add(map);
            }
            roleMapper.setRoleAndPermission(params);
        }
    }

    //    public void setRoleAndMenu(List<Integer> menuIds, Integer roleId) {
//        if (CollectionUtil.isNotEmpty(menuIds)) {
//              List<Map> params = new ArrayList<>();
//            for (Integer menuId : menuIds) {
//                Map map = new HashMap<>();
//                map.put("roleId", roleId);
//                map.put("menuId", menuId);
//            }
//            roleMapper.setRoleAndMenu(params);
//        }
//    }
}
